#include "gd32f30x.h"
#include "FreeRTOS.h"
#include "event_groups.h"
#include "timers.h"
#include "portmacro.h"
#include "queue.h"
#include "semphr.h"


uint16_t adcvalue = 0;

extern SemaphoreHandle_t Mutex_Handle; 
extern QueueHandle_t myqueue_Handle;
BaseType_t ret;
void ADC_Init(void)
{
    rcu_periph_clock_enable(RCU_ADC0);
    rcu_periph_clock_enable(RCU_GPIOA);
    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV4);

    gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_2);

    adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, ENABLE);
    adc_special_function_config(ADC0, ADC_SCAN_MODE, DISABLE);
    adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
    adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
    adc_mode_config(ADC_MODE_FREE);
    adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1);
    adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_2, ADC_SAMPLETIME_239POINT5);
    adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
    adc_resolution_config(ADC0, ADC_RESOLUTION_12B);
    adc_enable(ADC0);

    adc_calibration_enable(ADC0);    
}
void B5K_Task(void *parameters)
{
    adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
    for(;;)
    {
        adc_flag_clear(ADC0, ADC_FLAG_EOC);
        while(adc_flag_get(ADC0, ADC_FLAG_EOC) != SET){}
        adcvalue = adc_regular_data_read(ADC0);//adc采集到的数值在0~4095之间

        ret =  xQueueSend(myqueue_Handle, (void *)&adcvalue, pdMS_TO_TICKS(50));
        vTaskDelay(20);

    }
}
